package com.store.app.activity;

import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.Result;
import com.store.app.ExitApplication;
import com.store.app.LocationApplication;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.bean.EventBusBean;
import com.store.app.utils.GoToLogin;
import com.store.app.utils.ToastUtils;
import com.store.app.utils.scan.CameraPreview;
import com.store.app.zxing.Capture_ZxingActivity;
import com.zxing.decoding.camera.CameraManager;

import net.sourceforge.zbar.ImageScanner;

import de.greenrobot.event.EventBus;

/**
 * 提供给惠易定的扫描页面
 */

public class HYDCaptureActivity extends Capture_ZxingActivity {

    final String TAG = getClass().getSimpleName();

    private Camera mCamera;
    private CameraPreview mPreview;
    private CameraManager mCameraManager;
    private TextView title_tv;

    private Rect mCropRect = null;
    private boolean previewing = true;


    private Handler mHandler;

    private LinearLayout achieve;
    private Button btnCommit;
    private Button btnCancel;
    private ImageScanner mImageScanner = null;
    private TextView tv_cap;
    private GoToLogin goToLogin;
    private TextView tv_input_code;
    private TextView tv_light;
    private RelativeLayout rl_clean;
    private RelativeLayout rl_search;
    private EditText et_search_content;
    private TextView tv_search;
    private RelativeLayout capture_container;

    private void initView() {
        tv_input_code = (TextView) findViewById(R.id.tv_input_code);

        tv_light = (TextView) findViewById(R.id.tv_light);
        tv_light.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                controlCameraFlash();
            }
        });
        rl_clean = (RelativeLayout) findViewById(R.id.rl_clean);

        rl_search = (RelativeLayout) findViewById(R.id.rl_search);
        et_search_content = (EditText) findViewById(R.id.et_search_content);
        et_search_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (et_search_content.getText().toString().trim().length() > 0) {
                    rl_clean.setVisibility(View.VISIBLE);

                } else {
                    rl_clean.setVisibility(View.GONE);
                }
            }
        });
        tv_input_code.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                    if(rl_search.getVisibility()==View.VISIBLE){
                        rl_search.setVisibility(View.GONE);
                        tv_input_code.setBackgroundResource(R.drawable.bt_bg_white_empty_radius);
                    }else{
                        tv_input_code.setBackgroundResource(R.drawable.bt_bg_orange_radius);
                        et_search_content.requestFocus();
                        rl_search.setVisibility(View.VISIBLE);
                    }
            }
        });
        rl_clean.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                et_search_content.setText("");
            }
        });
        tv_search = (TextView) findViewById(R.id.tv_search);
        tv_search.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(et_search_content.getText().toString().trim())) {
                    ToastUtils.showTextToast(HYDCaptureActivity.this, "请输入条形码");
                } else {
                    EventBus.getDefault().post(new EventBusBean(202, et_search_content.getText().toString().trim()));
                    finish();
                }

            }
        });
    }

    private void showSearchPop() {

    }

    private void controlCameraFlash() {
        Camera.Parameters parameter = CameraManager.get().getCarmera().getParameters();
        String mode = parameter.getFlashMode();
        if ("off".equals(mode)) {
            tv_light.setBackgroundResource(R.drawable.bt_bg_orange_radius);
            Log.v("zyl", "当前闪光灯是关闭状态,去打开");
            parameter.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            CameraManager.get().getCarmera().setParameters(parameter);
        } else if ("torch".equals(mode)) {
            tv_light.setBackgroundResource(R.drawable.bt_bg_white_empty_radius);
            Log.v("zyl", "当前闪光灯是打开状态,去关闭");
            parameter.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            CameraManager.get().getCarmera().setParameters(parameter);
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_hyd_capture);
        initView();
        getWindow()
                .addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setInintView();
        findViewById();
    }


    private void findViewById() {
        findViewById(R.id.retreat).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        achieve = (LinearLayout) findViewById(R.id.achieve);
        btnCommit = (Button) findViewById(R.id.btn_commit);
        btnCancel = (Button) findViewById(R.id.btn_cancel);
        title_tv = (TextView) findViewById(R.id.title_tv);


        tv_cap = (TextView) findViewById(R.id.cap_tv);
        int width = LocationApplication.app.getDM().widthPixels;
        int high = LocationApplication.app.getDM().heightPixels;
        width = width / 8 * 3;
        high = high / 2 + width;
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.topMargin = high + MainActivity.dip2px(20);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        tv_cap.setLayoutParams(layoutParams);
        tv_cap.setText("扫描商品条形码,即可进货");
    }


    /**
     * 处理扫描结果
     *
     * @param result
     * @param barcode
     */
    public void handleDecode(Result result, Bitmap barcode) {
        inactivityTimer.onActivity();
        playBeepSoundAndVibrate();
        String resultString = result.getText();
        if (TextUtils.isEmpty(resultString)) {
            Toast.makeText(HYDCaptureActivity.this, "扫描错误!", Toast.LENGTH_SHORT).show();
        } else {
            if (!TextUtils.isEmpty(resultString)) {
                EventBus.getDefault().post(new EventBusBean(202, resultString));
                finish();
            }
        }
    }

    private void submit() {
        // validate
        String content = et_search_content.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            Toast.makeText(this, "请输入条形码", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something


    }
}
